package com.he1618.boot3.controller;


import com.nbsaas.boot.boot3.UserApi;
import com.nbsaas.boot.boot3.UserData;
import com.he1618.boot3.annotations.EncryptionData;
import com.he1618.boot3.event.TodoFinishEvent;
import com.nbsaas.boot.rest.annotations.CreateData;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.plexpt.chatgpt.ChatGPTStream;
import com.plexpt.chatgpt.entity.chat.Message;
import com.plexpt.chatgpt.listener.SseStreamListener;
import com.plexpt.chatgpt.util.Proxys;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.net.Proxy;
import java.util.Arrays;

@RestController
public class HelloController {


    @Resource
    private UserApi userApi;

    @Resource
    private  TaskScheduler taskScheduler;

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;


    @CreateData
    @RequestMapping("/message")
    public ResponseObject<String> message(String message) {


        return userApi.message(message);
    }


    @RequestMapping("/msg")
    public ResponseObject<String> msg(@RequestBody String message) {
        ResponseObject<String> result=new ResponseObject<String>();
        result.setData(message);
        return result;
    }

    @EncryptionData
    @RequestMapping("/msg1")
    public ResponseObject<String> msg1( @EncryptionData @RequestBody String message) {
        ResponseObject<String> result=new ResponseObject<String>();
        result.setData(message);
        return result;
    }
    @EncryptionData
    @RequestMapping("/msg2")
    public ResponseObject<String> msg2( @RequestBody String message) {
        ResponseObject<String> result=new ResponseObject<String>();
        result.setData(message);
        return result;
    }

    @RequestMapping("/msg3")
    public ResponseObject<String> msg3( @EncryptionData  @RequestBody String message) {
        ResponseObject<String> result=new ResponseObject<String>();
        result.setData(message);
        return result;
    }

    @RequestMapping("/user")
    public ResponseObject<UserData> user() {
        ResponseObject<UserData> result=new ResponseObject<UserData>();
        result.setData(UserData.builder().name("he1618").age(18).build());
        return result;
    }

    @RequestMapping("/user1")
    public ResponseObject<UserData> user(@RequestBody UserData userData) {
        ResponseObject<UserData> result=new ResponseObject<UserData>();
        result.setData(UserData.builder().name("he1618").age(userData.getAge()).build());
        return result;
    }



    @Value("${timeout:1000}")
    private int timeout;


    @RequestMapping("/index")
    public String index(){
        applicationEventPublisher.publishEvent(new TodoFinishEvent());
        return "i am ok  "+timeout;
    }




    @GetMapping("/chat/sse")
    @CrossOrigin
    public SseEmitter sseEmitter(String prompt) {
        //国内需要代理 国外不需要
        Proxy proxy = Proxys.http("127.0.0.1", 7890);

        ChatGPTStream chatGPTStream = ChatGPTStream.builder()
                .timeout(600)
                .apiKey("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")
                .proxy(proxy)
                .apiHost("https://api.openai.com/")
                .build()
                .init();

        SseEmitter sseEmitter = new SseEmitter(-1L);

        SseStreamListener listener = new SseStreamListener(sseEmitter);
        Message message = Message.of(prompt);

        listener.setOnComplate(msg -> {
            //回答完成，可以做一些事情
        });
        chatGPTStream.streamChatCompletion(Arrays.asList(message), listener);


        return sseEmitter;
    }

}
